package com.zhang.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhang.domain.*;
import com.zhang.mapper.PetDetailMapper;
import com.zhang.mapper.PetMapper;
import com.zhang.query.PetQuery;
import com.zhang.service.IPetService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @author zh
 * @create 2022-03-09 10:26
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS,readOnly = true)
public class PetServiceImpl implements IPetService {
    @Resource
    private PetMapper mapper;
    @Resource
    private PetDetailMapper detailMapper;
    @Transactional
    @Override
    public void save(Pet d) {
        d.setResources("http://34.80.47.178/"+d.getResources());
        d.getSearchMsg().setResources("http://34.80.47.178/"+d.getSearchMsg().getResources());
        mapper.saveMsg(d.getSearchMsg());
        User u = new User();
        u.setId(39L);
        d.setUser(u);
        Shop s = new Shop();
        s.setId(27L);
        d.setShop(s);
        d.setCreatetime(new Date());
        mapper.save(d);
    }
    @Transactional
    @Override
    public void update(Pet d) {
        if(!d.getResources().contains("http://34.80.47.178/")){
            d.setResources("http://34.80.47.178/"+d.getResources());
        }
        if(d.getSearchMsg().getResources()!=null&&!d.getSearchMsg().getResources().contains("http://34.80.47.178/")){
            d.getSearchMsg().setResources("http://34.80.47.178/"+d.getSearchMsg().getResources());
        }
        mapper.updateMsg(d.getSearchMsg());
        User u = new User();
        u.setId(39L);
        d.setUser(u);
        Shop s = new Shop();
        s.setId(27L);
        d.setShop(s);
        mapper.update(d);
    }
    @Transactional
    @Override
    public void delete(Long id) {
        mapper.deleteMsg(id);
        mapper.delete(id);
    }

    @Override
    public PageInfo<Pet> queryData(PetQuery query) {
        PageHelper.startPage(query.getCurrentPage(),query.getPageSize());
        List<Pet> Pets = mapper.queryData(query);
        Page<Pet> page = (Page<Pet>) Pets;
        return page.toPageInfo();
    }

    @Override
    public List<PetType> selAllPetType() {
        return mapper.selAllPetType();
    }

    @Override
    public PetDetail getPetDetail(Long id) {
        return detailMapper.getPetDetail(id);
    }

    @Override
    public Pet loadById(Long id) {
        return mapper.loadById(id);
    }
    @Transactional
    @Override
    public void updateState(long id) {
        mapper.updateState(id);
    }
    @Transactional
    @Override
    public void addOrder(PetAcq petAcq) {
        petAcq.setOrderSn(String.valueOf(System.currentTimeMillis()));
        petAcq.setUser_id(39L);
        petAcq.setShop_id(27L);
        petAcq.setState(0);
        petAcq.setLastcomfirmtime(new Date());
        mapper.addOrder(petAcq);
    }
}
